package cn.edu.zsc.rbac.service.array;

import cn.edu.zsc.rbac.domain.dto.UserDto;
import cn.edu.zsc.rbac.domain.entity.Dept;
import cn.edu.zsc.rbac.domain.entity.Role;
import cn.edu.zsc.rbac.domain.entity.User;
import cn.edu.zsc.rbac.domain.vo.Result;
import cn.edu.zsc.rbac.domain.vo.UserVo;
import cn.edu.zsc.rbac.service.IDeptService;
import cn.edu.zsc.rbac.service.IRoleService;
import cn.edu.zsc.rbac.service.IUserService;

import java.util.Arrays;

/**
 * 业务层-用户
 *
 * @author 吕淑言
 * @since 2025/5/11 15:19
 */
public class UserService extends BaseService<User, UserDto,  UserVo>implements IUserService {

    private final IDeptService deptService;
    private final IRoleService roleService;
    private static IUserService singleton;

    private UserService() {
        deptService = DeptService.getInstance();
        roleService = RoleService.getInstance();
        data = new User[DATA_SIZE+1];
        temp = new UserVo[DATA_SIZE];
    }
    public static IUserService getInstance() {
        if (singleton == null) {
            singleton = new UserService();
        }
        return singleton;
    }
    @Override
     UserVo convertToVo(User user) {
        String deptName= "";
        Dept dept=deptService.findById(user.deptId);
        if(dept!=null){
            deptName=dept.name;
        }
        String roleName= "";
        Role role=roleService.findById(user.roleId);
        if (role != null) {
            roleName = role.name;
        }
        return new UserVo(user.getId(), user.username, user.name, user.mobile, user.email, user.deptId, deptName, roleName, user.roleId
                , user.enabled);
    }
   @Override
    User createEntity(UserDto dto, int id) {
        return new User(id,dto.username, dto.name, dto.mobile, dto.email, dto.deptId, dto.roleId);
    }
    @Override
    void updateEntity(UserDto dto, User user) {
        user.username = dto.username;
        user.name = dto.name;
        user.mobile = dto.mobile;
        user.email = dto.email;
        user.deptId = dto.deptId;
        user.roleId = dto.roleId;
    }
    @Override
    public boolean deptInUse(int deptId){
        boolean inUse=false;
        for(User user:data){
            if(user!=null&&user.deptId==deptId){
                inUse=true;
                break;
            }
        }
        return inUse;
    }
    @Override
    public boolean roleInUse(int roleId){
        boolean inUse=false;
        for(User user:data){
            if(user!=null&&user.roleId==roleId){
                inUse=true;
                break;
            }
        }
        return inUse;
    }


    @Override
    public Result create(UserDto dto) {
        if (!deptService.existsById(dto.deptId)) {
            return Result.failure("创建失败，不存在指定部门");
        }
        if (!roleService.existsById(dto.roleId)) {
            return Result.failure("创建失败，不存在指定角色");
        }
        return super.create(dto);
    }
    @Override
    public Result update(UserDto dto, int id) {
        if (!deptService.existsById(dto.deptId)) {
            return Result.failure("创建失败，不存在指定部门");
        }
        if(!roleService.existsById(dto.roleId)){
            return Result.failure("创建失败，不存在指定角色");
        }
       return super.update(dto, id);
    }
    @Override
    public Result toggle(int id){
        if (!existsById(id)) {
            return Result.failure("切换状态失败，不存在指定用户");
        }
        data[id].enabled=!data[id].enabled;
        return Result.success(data[id].enabled?"启用成功":"停用成功", null);
    }
}
